import { platformApi } from '../../api';
import { PlatformTenantDto, PlatformTenantQuery } from '../../../types/platform';
/**
 * 租户API服务
 * 生产环境使用的真实API接口
 */
export const tenantApi = {

  
  /**
   * 分页查询租户列表（平台管理用）
   */
  getPlatformTenants: (params: PlatformTenantQuery) => {
    return platformApi.request<{
      records: PlatformTenantDto[];
      total: number;
      size: number;
      current: number;
      pages: number;
    }>({
      url: '/api/v3/platform/tenants/page',
      method: 'POST',
      data: params,
    });
  },

  /**
   * 根据ID获取租户详情（平台管理用）
   */
  getPlatformTenant: (id: string | number) => {
    return platformApi.request<PlatformTenantDto>({
      url: `/api/v3/platform/tenants/get/${id}`,
      method: 'GET',
    });
  },

  /**
   * 根据编码查询租户（平台管理用）
   */
  getPlatformTenantByCode: (code: string) => {
    return platformApi.request<PlatformTenantDto>({
      url: `/api/v3/platform/tenants/code/${code}`,
      method: 'GET',
    });
  },

  /**
   * 根据域名查询租户（平台管理用）
   */
  getPlatformTenantByDomain: (domain: string) => {
    return platformApi.request<PlatformTenantDto>({
      url: `/api/v3/platform/tenants/domain/${domain}`,
      method: 'GET',
    });
  },

  /**
   * 创建租户（平台管理用）
   */
  createPlatformTenant: (data: Partial<PlatformTenantDto>) => {
    return platformApi.request<PlatformTenantDto>({
      url: '/api/v3/platform/tenants/add',
      method: 'POST',
      data,
    });
  },

  /**
   * 更新租户（平台管理用）
   */
  updatePlatformTenant: (id: string | number, data: Partial<PlatformTenantDto>) => {
    return platformApi.request<PlatformTenantDto>({
      url: '/api/v3/platform/tenants/update',
      method: 'PUT',
      data: {
        ...data,
        id: id, // 确保请求体中包含租户ID
      },
    });
  },

  /**
   * 删除租户（平台管理用）
   */
  deletePlatformTenant: (id: string | number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/delete/${id}`,
      method: 'DELETE',
    });
  },

  /**
   * 暂停租户
   */
  suspendPlatformTenant: (id: string | number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/${id}/suspend`,
      method: 'POST',
    });
  },

  /**
   * 激活租户
   */
  activatePlatformTenant: (id: string | number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/${id}/activate`,
      method: 'POST',
    });
  },

  /**
   * 禁用租户
   */
  disablePlatformTenant: (id: string | number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/${id}/disable`,
      method: 'POST',
    });
  },

  /**
   * 获取租户使用情况
   */
  getPlatformTenantUsage: (id: string | number) => {
    return platformApi.request<{
      currentUsers: number;
      maxUsers: number;
      userUsageRate: number;
      expireTime: string;
      isExpiringSoon: boolean;
    }>({
      url: `/api/v3/platform/tenants/${id}/usage`,
      method: 'GET',
    });
  },

  /**
   * 延长租户有效期
   */
  extendPlatformTenantExpiry: (id: string | number, months: number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/${id}/extend`,
      method: 'POST',
      params: { months },
    });
  },

  /**
   * 升级租户套餐
   */
  upgradePlatformTenantPlan: (id: string | number, planType: string, maxUsers: number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/${id}/upgrade-plan`,
      method: 'POST',
      params: { planType, maxUsers },
    });
  },

  /**
   * 初始化租户Schema
   */
  initPlatformTenantSchema: (id: string | number) => {
    return platformApi.request({
      url: `/api/v3/platform/tenants/${id}/init-schema`,
      method: 'POST',
    });
  },



  /**
   * 检查编码是否存在
   */
  checkPlatformTenantCodeExists: (code: string, tenantId?: string | number) => {
    return platformApi.request<boolean>({
      url: '/api/v3/platform/tenants/check-code',
      method: 'GET',
      params: { code, tenantId },
    });
  },

  /**
   * 检查域名是否存在
   */
  checkPlatformTenantDomainExists: (domain: string, tenantId?: string | number) => {
    return platformApi.request<boolean>({
      url: '/api/v3/platform/tenants/check-domain',
      method: 'GET',
      params: { domain, tenantId },
    });
  },

  /**
   * 租户用户登录
   */
  login: (loginData: any) => {
    return platformApi.request({
      url: '/api/v3/tenant/auth/login',
      method: 'POST',
      data: loginData,
    });
  },

  /**
   * 获取公开租户列表
   */
  getPublicTenants: () => {
    return platformApi.request({
      url: '/api/v3/tenant/public/list',
      method: 'GET',
    });
  },

  /**
   * 搜索租户
   */
  searchTenants: (keyword: string) => {
    return platformApi.request({
      url: '/api/v3/tenant/public/search',
      method: 'GET',
      params: { keyword },
    });
  },
};

export default tenantApi;